<template>
    <view>
        <form @reset="formReset" @submit="formSubmit" :reportSubmit="true">
            <view class="container">
                <view class="head" v-if="ad.length > 0">
                    <swiper :autoplay="autoplay" :duration="duration" :indicatorDots="indicatorDots" :interval="interval">
                        <swiper-item v-for="(item, index) in ad" :key="index">
                            <image class="slide-image" mode="aspectFill" :src="item.logo"></image>
                        </swiper-item>
                    </swiper>
                </view>
                <view class="body">
									<view class="basic-info">
										<view class="title">
											基本信息
										</view>
										  <view class="text_list">
										      <view>商户名称</view>
										      <view>
										          <input name="name_title" placeholder="请输入店铺名称" :value="name" />
										      </view>
										  </view>
											
											  <view class="text_list" style="display: flex;align-items: center;">
											      <view>商户地址</view>
											      <view style="width: 50%;">
											          <text v-if="!address" style="color: #666;">请选择商户地址</text>
											          <text v-else>{{ address }}</text>
											      </view>
														<view @tap="choose_address" class="" style="color: #4B7BFB;margin-left: auto;width: 80rpx;">
															定位
														</view>
											  </view>
												
												<view class="text_list">
												    <view>联系人</view>
												    <view>
												        <input name="name_wor" placeholder="您的姓名" :value="link_name" />
												    </view>
												</view>
												
												<view class="text_list">
												    <view>联系电话</view>
												    <view>
												        <input @input="code" name="name_tel" placeholder="您的手机号" type="number" :value="link_tel" />
												    </view>
												</view>
												
												<view class="text_list" v-if="CheckSms.is_dxyz == 1">
												    <view>验证码</view>
												    <view>
												        <input name="code" placeholder="请输入短信验证码" type="number" />
												        <view @tap="sendmessg" class="code" v-if="getmsg == '获取验证码'" style="color: #4B7BFB;">{{ getmsg }}</view>
												        <view class="code" style="color: #999; font-size: 22rpx" v-else>{{ getmsg }}</view>
												    </view>
												</view>
									</view>
                    <view class="text_list introduction">
                        <view>简单介绍</view>
                        <view>
                            <textarea maxlength="100" name="name_prompt" placeholder="简单介绍下您的铺子" :value="details"></textarea>
                        </view>
                    </view>
                    <view class="upload_id">
                        <view>请上传商户LOGO</view>
                        <view style="text-align: center;">
                            <view @tap="choose" data-type="1" v-if="!upload_one" class="no-upload">
                                <image class="sele_img" mode="widthFix" src="/static/ymq_canyino2o/upload.png"></image>
                                logo图片
                            </view>
                            <view @tap="choose" data-type="1" v-else>
                                <image class="prompt_img" mode="widthFix" :src="bdupload_one"></image>
                            </view>
                        </view>
                    </view>
                    <view class="upload_id">
                        <view>请上传您的身份证正反面照片</view>
                        <view style="display: flex;flex-direction: column;align-items: center;">
                            <view @tap="choose" data-type="2" v-if="!upload_two" class="no-upload">
                                <image class="sele_img" mode="widthFix" src="/static/ymq_canyino2o/upload.png"></image>
                                身份证正面
                            </view>
                            <view @tap="choose" data-type="2" v-else>
                                <image class="prompt_img" mode="aspectFill" :src="bdupload_two"></image>
                            </view>
                            <view @tap="choose" data-type="3" v-if="!upload_three" class="no-upload">
                                <image class="sele_img" mode="widthFix" src="/static/ymq_canyino2o/upload.png"></image>
                                身份证反面
                            </view>
                            <view @tap="choose" data-type="3" v-else>
                                <image class="prompt_img" mode="widthFix" :src="bdupload_three"></image>
                            </view>
                        </view>
                    </view>
                    <view class="upload_id">
                        <view>请上传您的营业执照</view>
                        <view style="text-align: center;">
                            <view @tap="choose" data-type="4" v-if="!upload_four" class="no-upload">
                                <image class="sele_img" mode="widthFix" src="/static/ymq_canyino2o/upload.png"></image>
                                营业执照
                            </view>
                            <view @tap="choose" data-type="4" v-else>
                                <image class="prompt_img" mode="widthFix" :src="bdupload_four"></image>
                            </view>
                        </view>
                    </view>
                    <view class="ruzhu" v-if="array == true">
                        <view>合作时间</view>
                        <view>
                            <view @tap="sele_arr" :data-index="index" v-if="day == null" v-for="(item, index) in arr" :key="index">
                                <text>合作平台{{ item.days }}天</text>

                                <icon type="success" v-if="ac_index == index"></icon>

                                <icon type="cancel" v-else></icon>

                                <text :style="'color:' + color">￥{{ item.money }}</text>
                            </view>
                            <view v-if="day != null">
                                <text>合作平台{{ day }}天</text>
                                <icon type="success"></icon>
                                <text></text>
                            </view>
                        </view>
                    </view>
                    <view class="prompt">请仔细填写联系方式，保证我们能尽快联系到您</view>
                    <view class="protocol rel">
                        <icon @tap="selse_succ" class="ab ud" color="#4B7BFB" type="success" v-if="succ == true"></icon>
                        <icon @tap="selse_succ" class="ab ud" color="#4B7BFB" type="success" v-if="succ == false"></icon>
                        <text>我已阅读并了解</text>
                        <text @tap="xieyi" style="color: #4B7BFB;">【合作申请协议】</text>
                    </view>
                    <button formType="submit">立即申请合作</button>
                </view>
            </view>
        </form>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var app = getApp();
var siteinfo = require('../../../siteinfo.js');
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            imgUrls: [
                'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
                'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
                'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
            ],
            indicatorDots: false,
            autoplay: false,
            interval: 5000,
            duration: 1000,
            index: 0,
            getmsg: '获取验证码',
            ac_index: 0,
            succ: true,
            state: '',
            id: '',
            url: '',
            name: '',
            details: '',
            link_name: '',
            link_tel: '',
            address: '',
            latitude: '',
            phone: '',
            upload_one: '',
            upload_two: '',
            upload_three: '',
            upload_four: '',
            bdupload_one: '',
            bdupload_two: '',
            bdupload_three: '',
            bdupload_four: '',
            day: '',
            form_id: '',
            system: '',

            CheckSms: {
                is_dxyz: 0
            },

            ad: '',
            array: false,
            arr: '',
            num: '',
            send: false,
            color: ''
        };
    },
    onLoad: function (a) {
        app.globalData.setNavigationBarColor(this);
        var that = this;
        var e = uni.getStorageSync('users').id;
        that.setData({
            state: a.state
        });
        console.log(a);
        app.globalData.util.request({
            url: 'entry/wxapp/CheckRz',
            cachetime: '0',
            data: {
                user_id: e
            },
            success: function (a) {
                console.log(a.data);
                if (0 == a.data) {
                    var e = uni.getStorageSync('imglink');
                    that.setData({
                        id: '',
                        url: e
                    });
                    that.rz_time();
                } else {
                    that.setData({
                        name: a.data.name,
                        details: app.globalData.convertHtmlToText(a.data.details),
                        link_name: a.data.link_name,
                        link_tel: a.data.link_tel,
                        address: a.data.address,
                        latitude: a.data.coordinates,
                        phone: a.data.link_tel,
                        upload_one: a.data.logo,
                        upload_two: a.data.zm_img,
                        upload_three: a.data.fm_img,
                        upload_four: a.data.yyzz,
                        bdupload_one: a.data.logo,
                        bdupload_two: a.data.zm_img,
                        bdupload_three: a.data.fm_img,
                        bdupload_four: a.data.yyzz,
                        id: a.data.id,
                        day: a.data.rz_time,
                        url: ''
                    });
                    that.rz_time();
                }
            }
        });
        that.setData({
            form_id: a.form_id
        });
        app.globalData.util.request({
            url: 'entry/wxapp/system',
            cachetime: '0',
            success: function (a) {
                that.setData({
                    system: a.data
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/CheckSms',
            cachetime: '0',
            success: function (a) {
                that.setData({
                    CheckSms: a.data
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/ad',
            cachetime: '0',
            success: function (a) {
                console.log(a);
                var e = a.data;
                var t = [];
                for (var o in e) {
                    if ('5' == e[o].type) {
                        t.push(e[o]);
                    }
                }
                that.setData({
                    ad: t
                });
            }
        });
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {},
    methods: {
        rz_time: function (a) {
            var o = this;
            var that = this;
            app.globalData.util.request({
                url: 'entry/wxapp/GetRzqx',
                cachetime: '0',
                success: function (a) {
                    console.log(a);
                    var e = a.data;
                    if (0 < e.length) {
                        that.setData({
                            array: true,
                            arr: e
                        });
                        if (null != o.day) {
                            for (var t in e) {
                                if (e[t].days == o.day) {
                                    that.setData({
                                        ac_index: t
                                    });
                                }
                            }
                        }
                    } else {
                        that.setData({
                            array: false
                        });
                    }
                }
            });
        },

        selse_succ: function (a) {
            if (1 == this.succ) {
                this.setData({
                    succ: false
                });
            } else {
                this.setData({
                    succ: true
                });
            }
        },

        code: function (a) {
            console.log(a);
            this.setData({
                phone: a.detail.value
            });
        },

        sendmessg: function (a) {
            var that = this;
            console.log(that);
            var t = that.phone;
            if ('' == t || null == t) {
                uni.showModal({
                    title: '',
                    content: '请输入手机号'
                });
            } else {
                for (var o = '', s = 0; s < 6; s++) {
                    o += Math.floor(10 * Math.random());
                }
                console.log(o);
                app.globalData.util.request({
                    url: 'entry/wxapp/sms2',
                    cachetime: '0',
                    data: {
                        code: o,
                        tel: t
                    },
                    success: function (a) {
                        console.log(a);
                    }
                });
                that.setData({
                    num: o
                });
                var n = 59;
                var i = setInterval(function () {
                    that.setData({
                        getmsg: n + 's后重新发送',
                        send: true
                    });
                    if (--n <= 0) {
                        clearInterval(i);
                        that.setData({
                            getmsg: '获取验证码',
                            send: false
                        });
                    }
                }, 1000);
            }
        },

        bindPickerChange: function (a) {
            console.log('picker发送选择改变，携带值为', a.detail.value);
            this.setData({
                index: a.detail.value
            });
        },

        choose: function (a) {
            var that = this;
            var o = a.currentTarget.dataset.type;
            uni.getStorageSync('imglink');
            uni.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: function (a) {
                    console.log(a);
                    var e = a.tempFilePaths[0];
                    uni.uploadFile({
                        url: siteinfo.siteroot + '?i=' + siteinfo.uniacid + '&c=entry&a=wxapp&do=upload&m=ymq_canyino2o',
                        filePath: e,
                        name: 'upfile',
                        formData: {},
                        success: function (a) {
                            console.log('这是上传成功');
                            console.log(a);
                            if (1 == o) {
                                that.setData({
                                    bdupload_one: e,
                                    upload_one: a.data
                                });
                            } else {
                                if (2 == o) {
                                    that.setData({
                                        bdupload_two: e,
                                        upload_two: a.data
                                    });
                                } else {
                                    if (3 == o) {
                                        that.setData({
                                            bdupload_three: e,
                                            upload_three: a.data
                                        });
                                    } else {
                                        if (4 == o) {
                                            that.setData({
                                                bdupload_four: e,
                                                upload_four: a.data
                                            });
                                        }
                                    }
                                }
                            }
                        },
                        fail: function (a) {
                            console.log('这是上传失败');
                            console.log(a);
                        }
                    });
                }
            });
        },

        sele_arr: function (a) {
            this.arr;
            var e = a.currentTarget.dataset.index;
            if (3 == this.state || 4 == this.state) {
                uni.showModal({
                    title: '',
                    content: '合作期限不可以修改'
                });
            } else {
                this.setData({
                    ac_index: e
                });
            }
        },

        choose_address: function (a) {
            var that = this;
            uni.chooseLocation({
                success: function (a) {
                    console.log(a);
                    var e = a.latitude + ',' + a.longitude;
                    console.log(e);
                    that.setData({
                        address: a.address,
                        latitude: e
                    });
                }
            });
        },

        xieyi: function (a) {
            uni.navigateTo({
                url: 'xieyi'
            });
        },

        formSubmit: function (a) {
            var t = this;
            var e = a.detail.value;
            var o = e.name_title;
            var s = e.name_prompt;
            var n = e.name_wor;
            var i = e.name_tel;
            var l = e.code;
            var c = t.num;
            var d = t.arr;
            var u = t.succ;
            var r = t.ac_index;
            var p = t.array;
            var m = (t.system, t.id);
            if ('' == m) {
                console.log('这是新建');
                if (1 == p) {
                    var g = d[r].days;
                    var f = d[r].money;
                }
            } else {
                console.log('这是修改');
                g = t.day;
            }
            var _ = t.address;
            var h = t.upload_one;
            var y = t.upload_two;
            var w = t.upload_three;
            var x = t.upload_four;
            var v = t.latitude;
            var D = t.form_id;
            var z = t.CheckSms;
            var S = a.detail.formId;
            console.log(D);
            console.log(S);
            var k = uni.getStorageSync('users').id;
            app.globalData.util.request({
                url: 'entry/wxapp/AddFormId',
                cachetime: '0',
                data: {
                    user_id: k,
                    form_id: a.detail.formId
                },
                success: function (a) {
                    console.log(a.data);
                }
            });
            var T = uni.getStorageSync('users').openid;
            var q = '';
            if ('' == o) {
                q = '请输入商户名称';
            } else {
                if (null == _) {
                    q = '请选择商户地址';
                } else {
                    if ('' == s) {
                        q = '请输入商户简介';
                    } else {
                        if ('' == n) {
                            q = '请输入联系人姓名';
                        } else {
                            if ('' == i) {
                                q = '请输入联系人电话';
                            } else {
                                if (null == h) {
                                    q = '请上传商户logo';
                                } else {
                                    if (null == y) {
                                        q = '请上传身份证正面照片';
                                    } else {
                                        if (null == w) {
                                            q = '请上传身份证反面照片';
                                        } else {
                                            if (null == x) {
                                                q = '请上传营业执照';
                                            } else {
                                                if (c != l && 1 == z) {
                                                    q = '验证码输入错误';
                                                } else {
                                                    if (0 == u) {
                                                        q = '请先阅读并同意合作申请协议';
                                                    } else {
                                                        if (0 == p) {
                                                            q = '请选择合作期限';
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (1 == app.globalData.title(q)) {
                app.globalData.util.request({
                    url: 'entry/wxapp/SaveRzsq',
                    cachetime: '0',
                    data: {
                        id: m,
                        name: o,
                        user_id: k,
                        address: _,
                        details: s,
                        rz_time: g,
                        yyzz: x,
                        fm_img: w,
                        zm_img: y,
                        logo: h,
                        link_name: n,
                        link_tel: i,
                        coordinates: v,
                        money: f
                    },
                    success: function (a) {
                        console.log(a);
                        var e = a.data;
                        if ('' == m) {
                            if (0 < Number(f)) {
                                app.globalData.util.request({
                                    url: 'entry/wxapp/RzPay',
                                    cachetime: '0',
                                    data: {
                                        openid: T,
                                        money: f,
                                        rz_id: e
                                    },
                                    success: function (a) {
                                        uni.requestPayment({
                                            timeStamp: a.data.timeStamp,
                                            nonceStr: a.data.nonceStr,
                                            package: a.data.package,
                                            signType: a.data.signType,
                                            paySign: a.data.paySign,
                                            success: function (a) {
                                                console.log('支付成功');
                                                console.log(a);
                                                uni.showToast({
                                                    title: '申请已提交'
                                                });
                                                app.globalData.util.request({
                                                    url: 'entry/wxapp/RzMessage',
                                                    cachetime: '0',
                                                    data: {
                                                        form_id: D,
                                                        openid: T,
                                                        sh_id: e
                                                    },
                                                    success: function (a) {
                                                        console.log('发送模板消息');
                                                        console.log(a);
                                                    }
                                                });
                                                setTimeout(function () {
                                                    uni.navigateBack({
                                                        delta: 2
                                                    });
                                                }, 1500);
                                            },
                                            fail: function (a) {
                                                console.log('支付失败');
                                                uni.showToast({
                                                    title: '支付失败'
                                                });
                                                setTimeout(function () {
                                                    uni.navigateBack({
                                                        delta: 2
                                                    });
                                                }, 1500);
                                            }
                                        });
                                    }
                                });
                            } else {
                                app.globalData.util.request({
                                    url: 'entry/wxapp/RzMessage',
                                    cachetime: '0',
                                    data: {
                                        form_id: D,
                                        openid: T,
                                        sh_id: e
                                    },
                                    success: function (a) {
                                        console.log('发送模板消息');
                                        console.log(a);
                                    }
                                });
                                uni.showToast({
                                    title: '申请已提交'
                                });
                                setTimeout(function () {
                                    uni.navigateBack({
                                        delta: 2
                                    });
                                }, 1500);
                            }
                        } else {
                            uni.showToast({
                                title: '申请已提交'
                            });
                            setTimeout(function () {
                                uni.navigateBack({
                                    delta: 2
                                });
                            }, 1500);
                            app.globalData.util.request({
                                url: 'entry/wxapp/RzMessage',
                                cachetime: '0',
                                data: {
                                    form_id: D,
                                    openid: T,
                                    sh_id: t.id
                                },
                                success: function (a) {
                                    console.log('发送模板消息');
                                    console.log(a);
                                }
                            });
                        }
                    }
                });
            }
        },

        formReset() {
            console.log('占位：函数 formReset 未声明');
        }
    }
};
</script>
<style>
@import './info.css';
</style>
